package cn.south.campus.admin.config;

import cn.south.campus.admin.interceptor.LoginInterceptor;
import cn.south.campus.admin.interceptor.RefreshLoginInterceptor;
import cn.south.campus.admin.interceptor.TraceInterceptor;
import cn.south.campus.common.config.FileUploadConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2024/11/4 8:29
 */
@Component
public class MvcWebConfig implements WebMvcConfigurer {

    @Resource
    private LoginInterceptor loginInterceptor;
    @Resource
    private RefreshLoginInterceptor refreshLoginInterceptor;
    @Resource
    private TraceInterceptor traceInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/admin/login", "/admin/logout", "/files/**").order(2);
        registry.addInterceptor(refreshLoginInterceptor).excludePathPatterns("/admin/login", "/admin/logout", "/files/**").order(1);
        registry.addInterceptor(traceInterceptor).addPathPatterns("/**").order(0);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 本地文件上传路径
        String localFilePath = FileUploadConfig.LOCAL_FILE_PATH;
        String localFilePrefix = FileUploadConfig.LOCAL_FILE_PREFIX;
        registry.addResourceHandler("/" + localFilePrefix + "/**")
                .addResourceLocations("file:" + localFilePath + File.separator);
    }
}
